@echo off
CHCP 65001 > nul
echo [INSTALL] Installing HTTP Proxy locally...
echo.

REM Get GOPATH
for /f "tokens=*" %%i in ('go env GOPATH') do set GOPATH=%%i

if "%GOPATH%"=="" (
    echo [ERROR] GOPATH not found. Please ensure Go is properly installed.
    pause
    exit /b 1
)

echo [INFO] GOPATH: %GOPATH%

REM Create bin directory if it doesn't exist
if not exist "%GOPATH%\bin" (
    echo [ACTION] Creating %GOPATH%\bin directory...
    mkdir "%GOPATH%\bin"
)

REM Build the executable
echo [ACTION] Building HTTP Proxy...
go build -o http-proxy.exe ./cmd/http-proxy

if %errorlevel% neq 0 (
    echo [ERROR] Failed to build HTTP Proxy
    pause
    exit /b 1
)

REM Copy to GOPATH/bin
echo [ACTION] Installing to %GOPATH%\bin...
copy http-proxy.exe "%GOPATH%\bin\http-proxy.exe"

if %errorlevel% equ 0 (
    echo [SUCCESS] HTTP Proxy installed successfully!
    echo.
    echo You can now use 'http-proxy' command globally.
    echo Example: http-proxy
    echo.
    echo Note: Make sure your GOPATH/bin is in your system PATH.
    echo Current GOPATH/bin: %GOPATH%\bin
) else (
    echo [ERROR] Failed to install HTTP Proxy
    pause
    exit /b 1
)

echo.
echo [INFO] Installation completed!
pause